import instance from "../utils/request"
import {isValuePresent} from "@/utils/globalVariables";

const api = {
    //登录、用户
    login(user) {
        const formData = new FormData()
        formData.append('userName', user.username)
        formData.append('userPass', user.password)
        formData.append('codes', user.userCode)
        return instance.post('/login', formData)
            .then(response => {
                return response
            })
            .catch(error => {
                console.error(error)
            })
    },
    //注册
    insertUser(user) {
        const formData = new FormData()
        formData.append('userCode', user.usercode)
        formData.append('userPhone', user.userphone)
        formData.append('userName', user.username)
        formData.append('userPass', user.password)
        formData.append('codes', user.userCode)
        formData.append('userType', "3")
        return instance.post('/insertUser', formData)
            .then(response => {
                return response
            })
    },
    //修改手机号
    updateUser(user) {
        const formData = new FormData()
        formData.append('userId', user.userId)
        formData.append('userPhone', user.userPhone)
        formData.append('codes', user.userCode)
        return instance.post('/updateUser', formData)
            .then(response => {
                return response
            })
    },
    //虚拟物品
    getGoodsList() {
        return instance.get('/getGoods')
            .then(response => {
                return response
            })
    },
    //订单号排重
    checkOrderNo(orderNo) {
        const formData = new FormData()
        formData.append("orderNo", orderNo);
        return instance.post('/checkNo', formData)
            .then(response => {
                return response
            })
    },
    //支付确定
    checkPay(orderNo) {
        const formData = new FormData()
        formData.append("orderNo", orderNo);
        return instance.post('/checkOrderNo', formData)
            .then(response => {
                return response
            })
    },
    getPayCode(payVo) {
        const formData = new FormData()
        Object.entries(payVo).forEach(([key, value]) => {
            if (key === 'goodsCount') {
                return
            }
            if (isValuePresent(value)) {
                formData.append(key, value);
            }
        });
        return instance.post('/payLine', formData)
            .then(response => {
                return response
            })
    },
    checkPaymentStatus() {
        return instance.get('/checkPay')
            .then(response => {
                return response
            })
    },


    //订单
    getOrderList(userName) {
        return instance.get('/getOrderList?userName=' + userName)
            .then(response => {
                return response
            })
    },
}

export default api
